<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px;">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 开始写主体 -->
        <el-tree :data="list">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%;">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEidt(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children && data.children.length===0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 这里写弹窗组件 -->
      <el-dialog
        :title="isEdit? '编辑部门':'添加部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hclose"
      >
        <!-- 新增的编辑框，使用v-if是为了加载与删除组件（具有加载与删除功能） -->
        <addorEdit
          :id="curId"
          ref="myDept"
          :origin-list="originList"
          :is-edit="isEdit"
          @success="doSuccess"
          @doClose="showDialog = false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { loadTree } from '@/utils/index'
import { getDepartments, delDepartments } from '@/api/departments'
import addorEdit from './deptDialog.vue'

export default {
  components: {
    addorEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false, // 默认不显示弹窗
      isEdit: false, // false默认为新增,true默认为编辑
      curId: '',
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 1.列表数据的获取模块
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res)
        this.list = loadTree(res.data.depts)
        this.originList =
        // console.log(typeof (this.originList), '数据类型')为什么是obj
        this.originList = res.data.depts.map(item => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        console.log(this.originList, 'originList')
      } catch (error) {
        console.log(error)
      }
    },
    // 2.弹窗处理模块
    hAdd(id) {
      this.curId = id
      this.showDialog = true
      this.isEdit = false
    },
    hEidt(id) {
      this.curId = id
      this.showDialog = true
      this.isEdit = true
      // 推荐用这种方法nextTick
      this.$nextTick(() => {
        this.$refs.myDept.loadDeparamentDetail()
      })
    },
    hDel(id) {
      this.$confirm('此操作删除部门 是否继续?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartments(id)
        console.log(res, '删除部门')
        if (res.success) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.loadDepartments()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    doSuccess() {
      this.showDialog = false
      this.loadDepartments()
    },
    hclose() {
      this.$refs.myDept.resetForm()
    }

  }
}
</script>
